package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MinioUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.excel.ExcelUtils;
import com.ruoyi.system.domain.MinioFile;
import com.ruoyi.system.service.IMinioFileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * minio上传的文件Controller
 *
 * @author liuzhexian
 * @date 2023-06-04
 */
@RestController
@RequestMapping("/system/minioFile")
public class MinioFileController extends BaseController {

    @Value("${minio.bucketName}")
    private String bucketName;
    @Resource
    private IMinioFileService minioFileService;
    @Resource
    private MinioUtils minioUtils;

    /**
     * 查询minio上传的文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:list')")
    @GetMapping("/list")
    public TableDataInfo list(MinioFile minioFile) {
        startPage();
        List<MinioFile> list = minioFileService.selectMinioFileList(minioFile);
        return getDataTable(list);
    }

    /**
     * 导出minio上传的文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:export')")
    @Log(title = "minio上传的文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MinioFile minioFile) {
        List<MinioFile> list = minioFileService.selectMinioFileList(minioFile);
        ExcelUtils<MinioFile> util = new ExcelUtils<MinioFile>(MinioFile.class);
        util.exportData(response, list, "minio上传的文件数据");
    }

    /**
     * 获取minio上传的文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(minioFileService.selectMinioFileById(id));
    }

    /**
     * 新增minio上传的文件
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:add')")
    @Log(title = "minio上传的文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MinioFile minioFile) {
        return toAjax(minioFileService.insertMinioFile(minioFile));
    }

    /**
     * 修改minio上传的文件
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:edit')")
    @Log(title = "minio上传的文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MinioFile minioFile) {
        return toAjax(minioFileService.updateMinioFile(minioFile));
    }

    /**
     * 删除minio上传的文件
     */
    @PreAuthorize("@ss.hasPermi('system:minioFile:remove')")
    @Log(title = "minio上传的文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(minioFileService.deleteMinioFileByIds(ids));
    }


    /**
     * 方法描述： minio上传文件
     *
     * @param
     * @return
     * @author liuzhexian
     * @since 2023/6/4
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        MinioFile minioFile = new MinioFile();
        String imgPath = minioUtils.uploadFileSingle(bucketName, multipartFile);
        minioFile.setPath(imgPath.substring(0, imgPath.indexOf("?")));
        if (StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
            minioFile.setName(Objects.requireNonNull(
                            multipartFile.getOriginalFilename())
                    .substring(0, multipartFile.getOriginalFilename()
                            .lastIndexOf(".")));
        }
        minioFile.setCreateTime(new Date());
        return toAjax(minioFileService.insertMinioFile(minioFile));
    }
}
